﻿using iChip.Hotspots.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace iChip.Hotspots.Controllers
{
    public class AdminSplashsController : Controller
    {
        private readonly BO.BOs _bo = new BO.BOs();
        //
        // GET: /AdminSplashs/
        public ActionResult Index(int Hotspot_id, string Hotspot_mac, int? index)
        {
            var model = new List<Splash>();
            int pageSize = 20;
            int pageIndex = 1;
            if(index.GetValueOrDefault()>0)
            {
                pageIndex = index.GetValueOrDefault();
            }
            model = _bo.Select_Splash_Paging(pageIndex, pageSize, Hotspot_id);
            ViewBag.Hotspot_Mac = Hotspot_mac;
            ViewBag.Hotspot_Id = Hotspot_id;
            return View(model);
        }

        public ActionResult AddSplash(string returnUrl, int Hotspot_id, string Hotspot_mac, int? splash_Id)
        {
            var model = new SplashModel
            {
                Hotspot_Id = Hotspot_id,
                Hotspot_Mac = Hotspot_mac,
                Splash_Id = splash_Id.GetValueOrDefault()
            };
            if (splash_Id.GetValueOrDefault() > 0)
            {
                model = _bo.Select_Splash_by_Id(splash_Id.GetValueOrDefault());
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        [ValidateInput(false)]
        public async Task<ActionResult> AddSplash(SplashModel model, string returnUrl)
        {
            if(ModelState.IsValid)
            {
                bool flag = false;
                if (model.Splash_Id>0)
                {
                    flag = _bo.Update_Splashs(model);
                }
                else
                {
                    flag = _bo.Insert_Splashs(model);
                }

                return Redirect(returnUrl);
            }

            return View(model);
        }

	}
}